AddCSLuaFile( "sql_database.lua" )
include( 'sql_database.lua' )


function CheckDB() //Check our DataBase when initializing the game mode.
	if( tables_exist( "player_info" ) ) then
			print("player_info table exists!")
		else
			create_table( "CREATE TABLE player_info ( unique_id varchar(255), money int )" , "player_info" )
			print("creating player_info table...")
	end
			
	if( tables_exist( "player_skills" ) ) then
			print("player_skills table exists!")
				
		else 
			create_table( "CREATE TABLE player_skills ( unique_id varchar(255), stamina int, strength int, hp int )" , "player_skills" )
			print("creating player_skills table...")	
	end
end
hook.Add( "Initialize", "initializing", CheckDB );



function SetPlayerStats( ply )  // When spawning after joining the sever	
 
	timer.Create("Steam_id_delay", 2, 1, function() //Let the palyers steam id init
		SteamID = ply:SteamID()
		ply:SetNWString("SteamID", SteamID) //network string
		print("setplayerstats:steam id:" .. SteamID)
		end)
		timer.Create("SaveStat", 10, 0, TimerStat, ply) //create a timer that updates the palyers stats
		player_exists( ply )
		

	end
	
	function player_exists( ply )
 
	steamID = ply:GetNWString("SteamID")
 
	result = sql.Query("SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'")
	if (result) then
			sql_value_stats( ply ) // We will call this to retrieve the stats
			sql_value_skills( ply ) // We will call this to retrieve the skills
	else
		new_player( steamID, ply ) // Create a new player :D
		Msg("Created a new player.")
	end
end

 
function new_player( SteamID, ply )
 
		steamID = SteamID
		sql.Query( "INSERT INTO player_info (`unique_id`, `money`)VALUES ('"..steamID.."', '100')" )
		result = sql.Query( "SELECT unique_id, money FROM player_info WHERE unique_id = '"..steamID.."'" )
		if (result) then
 
			sql.Query( "INSERT INTO player_skills (`unique_id`, `stamina`, `strength`, `hp`)VALUES ('"..steamID.."', '1', '1', '1')" )
			result = sql.Query( "SELECT unique_id, stamina, strength, hp FROM player_skills WHERE unique_id = '"..steamID.."'" )
			if (result) then
				print("Player account created !\n")
				sql_value_stats( ply )
				sql_value_skills( ply )
			else
				print("Something went wrong with creating a players skills !\n")
			end
 
		else
			print("Something went wrong with creating a players info !\n")
		end
end


function sql_value_stats( ply )
	unique_id = sql.QueryValue("SELECT unique_id FROM player_info WHERE unique_id = '"..steamID.."'")
	money = sql.QueryValue("SELECT money FROM player_info WHERE unique_id = '"..steamID.."'")
	ply:SetNWString("unique_id", unique_id)
	ply:SetNWInt("money", money)
	

end
 
function sql_value_skills( ply )
	unique_id = sql.QueryValue("SELECT unique_id FROM player_skills WHERE unique_id = '"..steamID.."'")
	stamina = sql.QueryValue("SELECT stamina FROM player_skills WHERE unique_id = '"..steamID.."'")
	strength = sql.QueryValue("SELECT strength FROM player_skills WHERE unique_id = '"..steamID.."'")
	print(strength .. "from value stats")
	hp = sql.QueryValue("SELECT hp FROM player_skills WHERE unique_id = '"..steamID.."'")
	ply:SetNWString("unique_id", unique_id)
	ply:SetNWInt("stamina", stamina)
	ply:SetNWInt("strength", strength)
	ply:SetNWInt("hp", hp)
	

end
 
function saveStat ( ply )
	money = ply:GetNWInt("money")
	unique_id = ply:GetNWString ("SteamID")
	stamina = ply:GetNWInt("stamina")
	strength = ply:GetNWInt("strength")
	hp = ply:GetNWInt("hp")
	print(strength .. "from save stats")
	print("UPDATE player_skills SET stamina = "..stamina..", strength = "..strength..", hp = "..hp.." WHERE unique_id = '"..unique_id.."'")
	print("UPDATE player_info SET money = "..money.." WHERE unique_id = '"..unique_id.."'"	)
	sql.Query("UPDATE player_skills SET stamina = "..stamina..", strength = "..strength..", hp = "..hp.." WHERE unique_id = '"..unique_id.."'")
	sql.Query("UPDATE player_info SET money = "..money.." WHERE unique_id = '"..unique_id.."'")
	ply:ChatPrint("Stats updated!, set strength to " .. strength .. "in savestat")

end


////////Exp and money events///////////
	
	
	function TimerStat(ply)		//Timer based stat updating here
		Msg("Stat timer")
		
		//saveStat( ply ) 
		end
		
	
	function GM:OnNPCKilled( victim, ply, weapon )//Grants a user 1exp point toward their strength skill? when they kill a zombie
		if(victim:GetClass( ) == "npc_zombie" ) then
		print(ply:GetNWInt("strength"))
		str = ply:GetNWInt("strength")
		ply:SetNWInt("strength", (str + 1)) 
		saveStat( ply )
		end
	end
		
		
 
	
